package run.graphics;

import java.awt.Color;
import java.awt.Container;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

import run.ScenarioManager;
import run.ScenarioManager.SESSION;

public class StartDialog extends JDialog {

	private static final long serialVersionUID = 1707373683412976366L;
	
	protected ScenarioManager scenarioManager;
	
	public StartDialog(ScenarioManager sm, Frame owner) {
		
		super(owner, "Initialization", true);
		
		this.scenarioManager = sm;
		
		String[] sessions = new String[SESSION.values().length+1];
		sessions[0] = "";
		for (int i = 0 ; i < SESSION.values().length ; i++) {
			sessions[i+1] = SESSION.values()[i].toString();
		}
		
		JLabel participantLabel = new JLabel("Participant Id: ");
		JLabel trialLabel = new JLabel("Trial number: ");
		JLabel sessionLabel = new JLabel("Session: ");
		final JTextField participantTF = new JTextField(3);
		final JTextField trialTF = new JTextField(3);
		trialTF.setText("1");
		final JComboBox sessionChoice = new JComboBox(sessions);
		final JButton okButton = new JButton("OK");
		final JButton cancelButton = new JButton("Cancel");
		
		okButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String participant, trial;
				boolean ok = true;
				
				participant = participantTF.getText();
				trial = trialTF.getText();
				
				if (participant.length() == 0 || !participant.matches("\\d+")) {
					participantTF.setBorder(BorderFactory.createLineBorder(Color.red, 3));
					ok = false;
				} else {
					participantTF.setBorder(BorderFactory.createEmptyBorder());
				}
				
				if (trial.length() == 0 || !trial.matches("\\d+")) {
					trialTF.setBorder(BorderFactory.createLineBorder(Color.red, 3));
					ok = false;
				} else {
					trialTF.setBorder(BorderFactory.createEmptyBorder());
				}
				
				System.out.println(sessionChoice.getSelectedIndex());
				if (sessionChoice.getSelectedIndex() < 1) {
					sessionChoice.setBorder(BorderFactory.createLineBorder(Color.red, 3));
					ok = false;
				} else {
					sessionChoice.setBorder(BorderFactory.createEmptyBorder());
				}
				
				if (ok) {
					scenarioManager.setParticipant(Integer.parseInt(participant));
					scenarioManager.setSession(sessionChoice.getSelectedIndex()-1);
					scenarioManager.setCurrentTrial(Integer.parseInt(trial));
					
					scenarioManager.setCanStart(true);
					
					// dashboard.setParticipantNb(ScenarioManager.this.participant);
					// dashboard.setTrialNb(currentTrial);
					
					scenarioManager.initLogs();
					
					StartDialog.this.dispose();
				}
				
			}
			
		});
		
		KeyListener fieldsAdapter = new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				super.keyPressed(e);
				
				switch (e.getKeyCode()) {
				case KeyEvent.VK_ENTER:
					okButton.getActionListeners()[0].actionPerformed(null);
					break;

				case KeyEvent.VK_ESCAPE:
					cancelButton.getActionListeners()[0].actionPerformed(null);
					break;
					
				default:
					break;
				}
			}
		};
		
		participantTF.addKeyListener(fieldsAdapter);
		sessionChoice.addKeyListener(fieldsAdapter);
		trialTF.addKeyListener(fieldsAdapter);
		
		Container dialogContentPane = getContentPane();
		dialogContentPane.setLayout(new GridLayout(0, 2));
		dialogContentPane.add(participantLabel);
		dialogContentPane.add(participantTF);
		dialogContentPane.add(trialLabel);
		dialogContentPane.add(trialTF);
		dialogContentPane.add(sessionLabel);
		dialogContentPane.add(sessionChoice);
		dialogContentPane.add(cancelButton);
		dialogContentPane.add(okButton);
		
		pack();
		setVisible(true);
		
	}

}
